<?php
/**
 *  
 * @package admin
 * @author Mervin.Wong<mervin0502@163.com>
 * @Copyright
 * @license  http://www.apache.org/licenses/LICENSE-2.0
 * @version 0.1.0
 * @date 2013-2-23上午10:43:25
 *
 */
 
  /**
 * CanGoo Tag 控制器
 *
 *	标签管理模块
 *
 * @package		admin
 * @subpackage	Controllers
 * @category	Admin-controllers
 * @author		Mervin.Wong<mervin0502@163.com
 * @link		http://mervin.me/project/cangoo
 * 
 * 标签列表
 * 添加
 * 编辑
 * 删除
 */ 
 
 class Tag extends CI_Controller{
 	
 	/**
 	 * 构造函数
 	 */
 	public function __construct(){
 		parent::__construct();
 	  		$this->load->helper('url');
  		$this->load->library('session');
  		$this->load->model('common_model');
  		if(!$this->common_model->is_login($this->session->userdata('session_id'))){
  			redirect(site_url('index/login', 'refresh'));
  		}
 		$this->load->helper('date');
 		$this->load->helper('form');
 		
 		$this->load->model('tag_model');
 		
 		$this->load->library('form_validation');
 		/* 验证规则 */
 		$this->form_validation->set_rules('tag_name', '标签名', 'trim|required|min_length[2]|max_length[12]|callback_check[name]|xss_clean');//分类名
 		$this->form_validation->set_rules('tag_slug', '标签别名', 'trim|required|min_length[2]|max_length[12]|alpha_dash|callback_check[slug]|xss_clean');//分类别名
 		$this->form_validation->set_rules('tag_description', '标签描述', 'trim|required|xss_clean|prep_for_form');//分类描述
 		
 	}
 	
 	/**
 	 * 默认方法
 	 * @access public
 	 * @return void
 	 */
 	public function index(){
		//左侧列表
		$data['tag_list'] = $this->_tag_list(); 		
		
 		//右侧表单
		$tag_form = form_open('tag/add', array('class'=>'tag_form','id'=>'tag_form'));
 		$tag_form .= form_fieldset('添加标签');
 		$tag_form .= '<p>';
 		$tag_form .= form_label('标签名:','tag_name');
 		$tag_form .= form_input(array('name'=>'tag_name', 'id'=>'tag_name', 'value' => set_value('tag_name'), 'size'=>12));
 		$tag_form .= '</p><p>';
 		$tag_form .= form_label('标签别名:', 'tag_slug');
 		$tag_form .= form_input(array('name'=>'tag_slug', 'id'=>'tag_slug', 'value'=>set_value('tag_slug'), 'size'=>12));
 		$tag_form .= '</p><p>';
 		$tag_form .= form_label('标签描述:', 'tag_description', 'id="description"');
 		$tag_form .= form_textarea(array('name'=>'tag_description', 'id'=>'tag_description', 'rows'=>4, 'cols'=>20, 'value'=>set_value('tag_description')));
 		$tag_form .= '</p><p>';
 		$tag_form .= form_hidden('tag_id',0);//分类的ID
 		$tag_form .= form_hidden('is_submit', 'submit');//判断是否提交
 		$tag_form .= form_reset('tag_reset', '重写');
 		$tag_form .= form_submit('tag_submit', '提交');
 		$tag_form .= form_fieldset_close();
 		$tag_form .= form_close();
 		$data['tag_form'] = $tag_form;
 		$this->load->view('tag_view',$data);
 	}
 	
 	/**
 	 * 添加标签
 	 * @access public
 	 */
 	public function add(){
 		if($this->form_validation->run() == TRUE){
 			//通过检测
 			$this->_insert();
 			redirect(site_url('tag/index'));
 		}else{
 			//左侧列表
 			$data['tag_list'] = $this->_tag_list();
	 		//右侧表单
			$tag_form = form_open('tag/add', array('class'=>'tag_form','id'=>'tag_form'));
	 		$tag_form .= form_fieldset('添加标签');
	 		$tag_form .= '<p>';
	 		$tag_form .= form_label('标签名:','tag_name');
	 		$tag_form .= form_input(array('name'=>'tag_name', 'id'=>'tag_name', 'value' => set_value('tag_name'), 'size'=>12));
	 		$tag_form .= '</p><p>';
	 		$tag_form .= form_label('标签别名:', 'tag_slug');
	 		$tag_form .= form_input(array('name'=>'tag_slug', 'id'=>'tag_slug', 'value'=>set_value('tag_slug'), 'size'=>12));
	 		$tag_form .= '</p><p>';
	 		$tag_form .= form_label('标签描述:', 'tag_description', 'id="description"');
	 		$tag_form .= form_textarea(array('name'=>'tag_description', 'id'=>'tag_description', 'rows'=>4, 'cols'=>20, 'value'=>set_value('tag_description')));
	 		$tag_form .= '</p><p>';
	 		$tag_form .= form_hidden('tag_id',0);//分类的ID
	 		$tag_form .= form_hidden('is_submit', 'submit');//判断是否提交
	 		$tag_form .= form_reset('tag_reset', '重写');
	 		$tag_form .= form_submit('tag_submit', '提交');
	 		$tag_form .= form_fieldset_close();
	 		$tag_form .= form_close();
	 		$data['tag_form'] = $tag_form;
	 		$this->load->view('tag_view',$data); 			
 		}
 	}
 	
 	/**
 	 * 编辑标签
 	 * @access public
 	 * @param int $tid description
 	 */
 	public function edit($tid){
 		
 		if($this->input->post('is_submit') != 'submit'){
 			//没提交表单
 			//左侧列表
 			
 			$data['tag_list'] = $this->_tag_list();
 			$row = $this->tag_model->get_tag($tid);
 			//右侧表单
 			$tag_form = form_open('tag/edit/'.$tid, array('class'=>'tag_form','id'=>'tag_form'));
	 		$tag_form .= form_fieldset('编辑标签');
	 		$tag_form .= '<p>';
	 		$tag_form .= form_label('标签名:','tag_name');
	 		$tag_form .= form_input(array('name'=>'tag_name', 'id'=>'tag_name', 'value' => $row['name'], 'size'=>12));
	 		$tag_form .= '</p><p>';
	 		$tag_form .= form_label('标签别名:', 'tag_slug');
	 		$tag_form .= form_input(array('name'=>'tag_slug', 'id'=>'tag_slug', 'value'=> $row['slug'], 'size'=>12));
	 		$tag_form .= '</p><p>';
	 		$tag_form .= form_label('标签描述:', 'tag_description', 'id="description"');
	 		$tag_form .= form_textarea(array('name'=>'tag_description', 'id'=>'tag_description', 'rows'=>4, 'cols'=>20, 'value'=> $row['description']));
	 		$tag_form .= '</p><p>';
	 		$tag_form .= form_hidden('tag_id',$row['tid']);//分类的ID
	 		$tag_form .= form_hidden('is_submit', 'submit');//判断是否提交
	 		$tag_form .= form_reset('tag_reset', '重写');
	 		$tag_form .= form_submit('tag_submit', '提交');
	 		$tag_form .= form_fieldset_close();
	 		$tag_form .= form_close();
	 		$data['tag_form'] = $tag_form;
	 		$this->load->view('tag_view',$data);
 		}else{
 			//提交表单
 			if($this->form_validation->run() == TRUE){
 				$this->_update($tid);
 				redirect(site_url('tag/index'));
 			}else{
 				 //左侧列表
	 			$data['tag_list'] = $this->_tag_list();
		 		//右侧表单
				$tag_form = form_open('tag/edit/'.$tid, array('class'=>'tag_form','id'=>'tag_form'));
		 		$tag_form .= form_fieldset('编辑标签');
		 		$tag_form .= '<p>';
		 		$tag_form .= form_label('标签名:','tag_name');
		 		$tag_form .= form_input(array('name'=>'tag_name', 'id'=>'tag_name', 'value' => set_value('tag_name'), 'size'=>12));
		 		$tag_form .= '</p><p>';
		 		$tag_form .= form_label('标签别名:', 'tag_slug');
		 		$tag_form .= form_input(array('name'=>'tag_slug', 'id'=>'tag_slug', 'value'=>set_value('tag_slug'), 'size'=>12));
		 		$tag_form .= '</p><p>';
		 		$tag_form .= form_label('标签描述:', 'tag_description', 'id="description"');
		 		$tag_form .= form_textarea(array('name'=>'tag_description', 'id'=>'tag_description', 'rows'=>4, 'cols'=>20, 'value'=>set_value('tag_description')));
		 		$tag_form .= '</p><p>';
		 		$tag_form .= form_hidden('tag_id',$tid);//分类的ID
		 		$tag_form .= form_hidden('is_submit', 'submit');//判断是否提交
		 		$tag_form .= form_reset('tag_reset', '重写');
		 		$tag_form .= form_submit('tag_submit', '提交');
		 		$tag_form .= form_fieldset_close();
		 		$tag_form .= form_close();
		 		$data['tag_form'] = $tag_form;
		 		$this->load->view('tag_view',$data); 
 			}
 		}
 		
 	}
 	
 	
 	public function del($tid){
 		$this->_delete($tid);
 		redirect(site_url('tag/index'));
 	}
 	/**
 	 * 服务器端标签名和别名重复性检测
 	 * @access public
 	 * @param string $value 表单域值
 	 * @param string $field 在数据库中要检测的字段
 	 * @return bool description
 	 */
 	public function check($value, $field){
  	 	if($this->tag_model->check($value, $field, $this->input->post('tag_id'))){
 	 		//第三个参数表示该标签不是添加动作
 	 		// 存在
 	 		$this->form_validation->set_message('check', '%s:"'.$value.'"已存在！');
 	 		return false;
 	 	}else{
 	 		return true;
 	 	}		
 	}
 	
 	
 	
 	/**
 	 * 数据库插入操作
 	 * @access private
 	 */
 	private function _insert(){
 		$args['name'] = $this->input->post('tag_name');
 		$args['slug'] = $this->input->post('tag_slug');
 		$args['description'] = $this->input->post('tag_description');
 		
 		$args['module'] = 'blog';
 		$args['type'] = 'tag';
 		$args['parent'] = 0;
 		$args['isDefault'] = 0;
 		$args['count'] = 0;
 		$args['addTime'] = now();
 		$args['modTime'] = now();
 		return $this->tag_model->insert($args);
 	}
 	
 	private function _update($tid){
 		if($tid == $this->input->post('tag_id')){
 			//通过GET传递的ID与表单隐藏项的值相同
 			$args['name'] = $this->input->post('tag_name');
 			$args['slug'] = $this->input->post('tag_slug');
 			$args['description'] = $this->input->post('tag_description');
 			$args['modTime'] = now();
 			return $this->tag_model->update($args,$tid);
 		}else{
 			return false;
 		}
 	}
 	
 	private function _delete($tid){
 		return $this->tag_model->delete($tid);
 	}
 	
 	/**
 	 * 获取标签列表
 	 */
 	private function _tag_list(){
 		return $this->tag_model->get_tags(array('tid', 'name', 'slug', 'description', 'count'));
 	}
 	

 }
/* End of file tag.php */
/* Loaction : ./apps/admin/controllers/tag.php*/
